package com.riddle.admin.controller;

import java.util.List;

import com.riddle.model.Menu;
import com.riddle.service.BaseService;
import com.riddle.util.JsonUtil;
import com.riddle.util.StringUtil;
import com.riddle.vmodel.PageModel;
import com.riddle.vmodel.QueryModel;
import com.riddle.vmodel.VMsg;

public abstract class BaseController<T> {

	protected abstract BaseService getService();
	
	public String list(QueryModel queryModel) {
		queryModel.setQuery(StringUtil.urlDecode(queryModel.getQuery()));
		List<Menu> list = getService().queryModelByPage(queryModel.getPage(), queryModel.getRows(), queryModel.getQuery());
		PageModel model = new PageModel(list.size(), list);
		return JsonUtil.toJson(model);
	}
	
	public String addModel(T t) {
		getService().insert(t);
		VMsg msg = new VMsg();
		msg.setMessage("success");
		return JsonUtil.toJson(msg);
	}
	
	public String modifyModel(T t) {
		getService().update(t);
		VMsg msg = new VMsg();
		msg.setMessage("success");
		return JsonUtil.toJson(msg);
	}
	
	public String delete(Long id) {
		getService().delete(id);
		VMsg msg = new VMsg();
		msg.setMessage("success");
		return JsonUtil.toJson(msg);
	}
	
	public String getParentModel() {
		return JsonUtil.toJson(getService().queryParent());
	}
}
